package com.rongma.waterdrop.manager.bmobmanager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.rongma.waterdrop.db.WaterDropHelper;
import com.rongma.waterdrop.global.TaskStatus;
import com.rongma.waterdrop.global.WaterDropApplication;
import com.rongma.waterdrop.model.Task;

import java.util.ArrayList;

/**
 * 任务实体类的数据库操作
 */
public class TaskManager
{
    private static final String DB_NAME = "task";
    private static final String COLUMN_TITLE = "planTitle";
    private static final String COLUMN_CONTENT = "planContent";
    private static final String COLUMN_STATUS = "status";
    private static final String COLUMN_SCORE = "score";
    private static final String COLUMN_UPLOAD = "upload";

    private static final TaskManager manager = new TaskManager();

    private static WaterDropHelper waterDropHelper;

    private TaskManager()
    {
    }

    public static TaskManager getManager(Context context)
    {
        waterDropHelper = new WaterDropHelper(context);
        return manager;
    }

    //添加task到数据库
    public void saveTask(Task task)
    {
        if(task != null)
        {
            SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
            sdb.beginTransaction();
            try
            {
                ContentValues contentValues = new ContentValues();
                contentValues.put(COLUMN_TITLE,task.getPlanTitle());
                contentValues.put(COLUMN_CONTENT,task.getPlanContent());
                contentValues.put(COLUMN_SCORE,task.getScore());
                contentValues.put(COLUMN_STATUS,task.getStatus());
                contentValues.put(COLUMN_UPLOAD,0);
                sdb.insert(DB_NAME,null,contentValues);
                sdb.setTransactionSuccessful();
            }
            finally
            {
                sdb.endTransaction();
                sdb.close();
            }
        }
    }

    //批量添加Task数据到数据库
    public void addTaskListToDB(ArrayList<Task> tasks)
    {
        SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
        sdb.beginTransaction();
        ContentValues contentValues = new ContentValues();
        try
        {
            for(Task task : tasks)
            {
                contentValues.put(COLUMN_TITLE,task.getPlanTitle());
                contentValues.put(COLUMN_CONTENT,task.getPlanContent());
                contentValues.put(COLUMN_SCORE,task.getScore());
                contentValues.put(COLUMN_STATUS,task.getStatus());
                contentValues.put(COLUMN_UPLOAD,0);
                sdb.insert(DB_NAME,null,contentValues);
                contentValues.clear();
            }
            sdb.setTransactionSuccessful();
        }
        finally
        {
            sdb.endTransaction();
            sdb.close();
        }
    }

    //从数据库中删除任务
    public void deleteTask(Task task)
    {
        SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
        sdb.beginTransaction();
        try
        {
            sdb.delete(DB_NAME,COLUMN_TITLE+"=?",new String[]{task.getPlanTitle()});
            sdb.setTransactionSuccessful();
        }
        finally
        {
            sdb.endTransaction();
            sdb.close();
        }
    }

    //删除所有已经完成的任务
    public void deleteAllFinishTask()
    {
        SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
        sdb.beginTransaction();
        try
        {
            sdb.delete(DB_NAME,COLUMN_STATUS+"=?",new String[]{Integer.toString(TaskStatus.FINISH)});
        }
        finally
        {
            sdb.endTransaction();
            sdb.close();
        }
    }

    //修改任务状态为已完成
    public void changeStatus(Task task)
    {
        if(task != null)
        {
            SQLiteDatabase sdb = waterDropHelper.getWritableDatabase();
            sdb.beginTransaction();
            try
            {
                ContentValues contentValues = new ContentValues();
                contentValues.put(COLUMN_STATUS,TaskStatus.FINISH);
                sdb.update(DB_NAME,contentValues,COLUMN_TITLE+"=?",new String[]{task.getPlanTitle()});
                sdb.setTransactionSuccessful();
            }
            finally
            {
                sdb.endTransaction();
                sdb.close();
            }
        }
    }


    //查询所有的Task
    public ArrayList<Task> queryAllTask()
    {
        ArrayList<Task> tasks = new ArrayList<Task>();
        SQLiteDatabase sdb = waterDropHelper.getReadableDatabase();
        Cursor cursor = sdb.query(DB_NAME,null,COLUMN_STATUS+"=?",new String[]{Integer.toString(TaskStatus.WAITING)},null,null,null);
        try
        {
            addTaskToList(cursor,tasks);
            cursor = sdb.query(DB_NAME,null,COLUMN_STATUS+"=?",new String[]{Integer.toString(TaskStatus.FINISH)},null,null,null);
            addTaskToList(cursor,tasks);
        }
        finally
        {
            cursor.close();
            sdb.close();
        }
        return tasks;
    }

    //添加Task到数据集合
    private void addTaskToList(Cursor cursor,ArrayList<Task> tasks)
    {
        if(cursor.moveToFirst())
        {
            do {
                Task task = new Task();
                task.setPlanTitle(cursor.getString(cursor.getColumnIndex(COLUMN_TITLE)));
                task.setPlanContent(cursor.getString(cursor.getColumnIndex(COLUMN_CONTENT)));
                task.setScore(cursor.getInt(cursor.getColumnIndex(COLUMN_SCORE)));
                task.setStatus(cursor.getInt(cursor.getColumnIndex(COLUMN_STATUS)));
                tasks.add(task);
            } while (cursor.moveToNext());
        }
    }


}
